{% extends "base.html" %}

{% block pagedef %} Upload collection > Statistics {% endblock %}

{% block script %}
<script type="text/javascript"> 

function get_progress_suggestions(){
    var refreshId1 = setInterval(function() {
        $.getJSON("progressbar", {}, function(data){
            $('#progressbar_badtaxa').progressBar(data.suggestions);
            if( data.suggestions >= 100 ){ clearInterval(refreshId1);}
        });
    }, 5000);
} 

function get_progress_tree(){
    var refreshId2 = setInterval(function() {
        $.getJSON("progressbar", {}, function(data){
            $('#progressbar_reference').progressBar(data.reference_tree);
            if( data.reference_tree == "100" ){ clearInterval(refreshId2);}
        });
    }, 5000);
} 

$(document).ready(function() { 
    hxr_suggestion = $.ajax({
        method: "get",url: "suggestions",
        beforeSend: function(html){
            get_progress_suggestions();
            $("#suggestions").html( '<b><font color="red">Searching correct names similar to those of unrecognized taxa...</font></b><br />');
        },
        complete: function(){ $("#progressbar_badtaxa").hide("fast");},
        success: function(html){
            $("#suggestions").html(html);
        }
    });

    hxr_reference = $.ajax({
        method: "get",url: "reference_tree",
        beforeSend: function(html){
            get_progress_tree();
            $('#reference_tree').html('<b><font color="red">Drawing reference tree...</font></b><br />');
        },
        complete: function(){ $("#progressbar_reference").hide("fast");},
        success: function(html){
            $("#reference_tree").html(html);
        }
    });

    $('.path').click( function(){
        hxr_suggestion.abort();
        hxr_reference.abort();
    });
}); 


$(document).ready(function(){
//    $("#exemplequery").hide();
    $("#exemplelink").click(function () {
        $("#exemplequery").slideToggle("slow");
    });
});
function unselectAll(){
    $(document).ready( function(){
       $("input.restrict").removeAttr( "checked" );
    });
}
function selectAll(){
    $(document).ready( function(){
       $("input.restrict").attr( "checked", "checked" );
    });
}
function selectGenre( genre ){
    $(document).ready( function(){
        if( $("span[genre="+genre+"] input").attr("check")){
           $("span[genre="+genre+"] input").removeAttr( "check" );
           $("span[genre="+genre+"] input").removeAttr( "checked" );
        }
        else{
            $("span[genre="+genre+"]  input").attr("check", "check");
            $("span[genre="+genre+"]  input").attr("checked", "checked");
        }
    });
}

$(document).ready(function() { 
    var error_msg = "{{error_msg|safe}}";
    if (error_msg){
        $('#error').jGrowl( "<b>"+error_msg+"</b>", { life: 10000 } );
    }
});

$(document).ready(function() { 
    $('button').click( function(){
        $('.alone').attr('checked', 'checked');
    });
});

$(document).ready( function(){
    $('#tab_reference_tree').tabs();
});

$(document).ready( function(){
    $('#query_filter').tabs();
});


$(document).ready(function(){
    $("#image_load").hide();
    var col_id = $("#col_id").attr( 'value' );
    $.get("get_phyfi_reference_tree_image_url/"+col_id+"?internal_label=1", {}, function(data){
        $("#image_tree .img").attr( "src", data );
        $("#fetching_msg").hide();
        $("#image_load").show();
    });
});

function add_item_filter(idfilter){
    var val = $("#taxa_input").attr('value');
    if (val){
        idfilter += 1;
        $("#filter_taxa").append( '<li class="li" id="li'+idfilter+'">'+val+'&nbsp; <a onclick="javascript:remove_filter('+idfilter+')" href="#filter" style="color:grey" title="remove">[X]</a></li>' );
        $("#filter_submit").append( '<input type="hidden" name="'+val+'" value="'+val+'" />' );
        $("#taxa_input").removeAttr( 'value' );
        $('#filter_submit').show();
    }
    else{ alert( "You must enter a valid taxon name" );}
}


function remove_filter( idfilter ){
    $(document).ready( function(){
        if( $(".li").length == 1){
            $('#filter_submit').hide();
       }
       $("#li"+idfilter).remove(); 
    });
}

$(document).ready(function(){
    $('#filter_submit').hide();
    var idfilter = 0;
    $("#add_filter").click( function(){ add_item_filter(idfilter); });
    $("#taxa_input").keypress(function(e){
        if( e.which == 13 ){ add_item_filter(idfilter) ; }
    });
});

$(document).ready(function(){
    setAutoComplete("taxa_input", "results", "/phyloexplorer/autocomplete?query=");
});

function toggle_exemple(){
    var checked = $("#against_treebase").attr( "checked" );
    if (checked){
        $("#exemplequery").hide();
        $("#exempletreebase").show();
    }
    else{
        $("#exemplequery").show();
        $("#exempletreebase").hide();
    }
}

$(document).ready(function(){
    toggle_exemple();
    $("#against_treebase").click( function(){
        toggle_exemple();
    });
});

</script>
{% endblock %}

{% block idpage %}/phyloexplorer/statistics{% endblock %}

{% block content %}
<a href="#bottom" class="posPage">&nbsp;Go to the bottom of the page&nbsp;</a>
<br />
<!-- CONTENU : BEGINNING____________________________________________________________________ -->
<input id="col_id" type="hidden" value="{{current_col_id}}" name="col_id" />
<div class="stat">
{% if not bad_tree_msg %}
<div id="query_filter">
    <ul>
        <li><a href="#query"><span>Search trees</span></a></li>
        <li><a href="#filter"><span>Prune trees</span></a></li>
    </ul>
    <div id="query">
        <!-- query panel -->
        <div class="query">
        <form action="statistics" class="nopadding">
        Here you can obtain a new tree collection by searching trees
        satisfying a taxonomic query in either your collection or in
        Treebase.<br />
        <br />
        <input size=60  type=text 
        onfocus="if(this.value=='Your query...'){this.value='';}" 
        onblur="if(this.value==''){this.value='Your query...';}"
        {% if query %} value="{{query}}" {% else %} value="Your query..." {% endif %}
        name="query" /> 
        <input id="against_treebase" type="checkbox" {% ifequal treebase "on" %} checked="checked" {% endifequal %} name="treebase"/> query against Treebase
        <input type="submit" value="Search" class="button" blockui="wait"> <br>
        <tt><span id="exemplequery"> query example: <b> {muridae} > 25  and ({mus}==1 or {rattus}==1) </b></span>
        <span id="exempletreebase">
        query example: <b>{usertaxa} > 5 and {carnivora}==1 and not {primates}</b>
        <br />
        <br /> 
        When searching Treebase you can use the special query term "{usertaxa}" to define the taxa present <br />
        in your current collection and thus searching for trees from Treebase overlapping with this collection.
        </span>
        <br /> 
        <br />
        <i>You can download your result after your query</i></tt>
        </form>
        {% if collection_changed %}
            Results for --&nbsp; {{query}} &nbsp;--&nbsp;|&nbsp;<a blockui="wait" href=/phyloexplorer/statistics?clear_collection=1>Back to original collection</a>
        {% endif %}
        </div>
        <!-- end query panel -->
    </div>
    <div id="filter">
        <!-- filter panel -->
        <div id="filter" class="query">
        <center>
        <table>
        <tr>
            <center>Here you can focus on a species subset by pruning your trees and keeping only taxa of interest</center>
        </tr>
        <tr>
        <td>
            <input id="taxa_input" size=40  type=text /> 
            <a id="add_filter" href="#filter"> Add to filter list </a>
            <br />
            <div id="autocomplete_results"></div>
            <i>You can download your result after filtering</i></tt>
        </td>
        <td>
            <div>
            <ul id="filter_taxa"> </ul>
            </div>
        </td>
        </tr>
        </table>
        <center>
        <form action="filter_collection" class="nopadding">
        <div id="filter_submit">
            <b>
            prune your trees and keep only taxa
            <select name="filter_option">
                <option select>in</option>
                <option>not in</option>
            </select>
            the filter list
            <input type="submit" value="Restrict your trees" blockui="wait">
            </b>
        </div>
        </form>
        </center>
        {% if collection_changed %}
            Results for --&nbsp; {{query}} &nbsp;--&nbsp;|&nbsp;<a blockui="wait" href=/phyloexplorer/statistics?clear_collection=1>Back to original collection</a>
        {% endif %}
        </center>
        </div>
        <!-- end filter panel -->
    </div>
</div>
{% endif %}

<div  id="error"></div>
{% if bad_tree_msg %} <div class="error">{{ bad_tree_msg|safe }}</div>{% endif %}
{% if not_empty_collection %}
</div>
    <br />
    <a name="arbo"></a>
    <fieldset>
        <legend> General information </legend>
	{{nb_user_labels}} different label(s) occur(s) in {{ nb_trees}} tree(s) <br />
       	<ul>
	<li>{{nb_valid_user_labels}}  
	<!--{{nb_user_taxa}} taxa in {{ nb_trees}} trees <br />
        {{ nb_taxa }} distinct OTU (Operational Taxonomic Unit) &nbsp;-->
    {% if nb_homonyms or nb_synonyms or nb_commons %}
	(including:
    {% endif %}
    {% if nb_homonyms %}
        <font color="orange">{{ nb_homonyms}} homonym(s) </font>
    {% endif %}
    {% if nb_synonyms %}
        <font color="orange">{{ nb_synonyms}} synonym(s) </font>
    {% endif %}
    {% if nb_commons %}
        <font color="orange">{{ nb_commons}} vernacular(s) </font>
    {% endif %}
    {% if nb_homonyms or nb_synonyms or nb_commons %}
	)
    {% endif %}
     are successfuly mapped to {{nb_user_taxa}} different taxa of the reference taxonomy</li>
    {% if nb_badtaxa %}
        <li> <font color="red"> {{ nb_badtaxa }} can not be mapped to the reference taxonomy  </font>&nbsp;</li>
    {% endif %}
	</ul>
</fieldset>
<br />

{% if nb_homonyms or nb_synonyms or nb_commons or nb_badtaxa %}
    <!-- Collection correction -->
    <fieldset>
    <legend> Correct your collection </legend>
    <button>Select all single suggestions</button>
    <form action="recreate_collection">
    <!-- homonym fieldset -->
    {% if nb_homonyms %}
        <span class="homonymlist">
        <fieldset>
        <legend> Homonym names to scientific names </legend>
        <table>
        <!-- <tr><th>user taxa name</th><th>related scientific names</th></tr> -->
        {% for homonym_name, suggestions in dict_homonyms.iteritems %}
            <tr>
            <td>
            <font color="orange">{{ homonym_name }}</font> :
            </td>
            <td>
            {% for sugg in suggestions %}
                {{ sugg }}
                <input 
                {% if suggestions|length_is:"1" %}
                type="checkbox" class="alone"
                {% else %}
                type="radio"
                {% endif %}
                name="{{homonym_name}}" value="{{ sugg }}" />
            {% endfor %}
            </td>
            </tr>
        {% endfor %}
        </table>
        </fieldset>
        </span>
    {% endif %}
    <!-- end homonym fieldset -->
    <!-- synonym fieldset -->
    {% if nb_synonyms %}
        <span class="homonymlist">
        <fieldset>
        <legend> Synonym names to scientific names </legend>
        <table>
        <!-- <tr><th>user taxa name</th><th>related scientific names</th></tr> -->
        {% for synonym_name, suggestions in dict_synonym.iteritems %}
            <tr>
            <td>
            <font color="orange">{{ synonym_name }}</font> :
            </td>
            <td>
            {% for sugg in suggestions %}
                {{ sugg }}
                <input 
                {% if suggestions|length_is:"1" %}
                type="checkbox" class="alone"
                {% else %}
                type="radio"
                {% endif %}
                name="{{synonym_name}}" value="{{ sugg }}" />
            {% endfor %}
            </td>
            </tr>
        {% endfor %}
        </table>
        </fieldset>
        </span>
    {% endif %}
    <!-- end synonym fieldset -->
    <!-- common fieldset -->
    {% if nb_commons %}
        <span class="homonymlist">
        <fieldset>
        <legend> Common names to scientific names </legend>
        <table>
        <!-- <tr><th>user taxa name</th><th>related scientific names</th></tr> -->
        {% for common_name, suggestions in dict_common.iteritems %}
            <tr>
            <td>
            <font color="orange">{{ common_name }}</font> :
            </td>
            <td>
            {% for sugg in suggestions %}
                {{ sugg }}
                <input 
                {% if suggestions|length_is:"1" %}
                type="checkbox" class="alone"
                {% else %}
                type="radio"
                {% endif %}
                name="{{common_name}}" value="{{ sugg }}" />
            {% endfor %}
            </td>
            </tr>
        {% endfor %}
        </table>
        </fieldset>
        </span>
    {% endif %}
    <!-- end common fieldset -->
    <!-- badtaxa fieldset -->
    {% if nb_badtaxa %}
        <fieldset>
        <legend> {{ nb_badtaxa }} label(s) can not be mapped to the reference taxonomy</legend>
	 Due to computing times, suggestions are proposed only for the {{ NB_SUGGESTION_SEARCH }} first unrecognized taxa.</br>
        <span id="suggestions" class="homonymlist"> </span>
        <div id="progressbar_badtaxa"></div>
        </fieldset>
        {% if display_button %}
            <input type="submit" value="Correct collection"/>
        {% endif %}
    {% endif %}
    <!-- end badtaxa fieldset -->
    {% if nb_commons or nb_synonyms or nb_homonyms or nb_badtaxa %}
        <input type="submit" value="Correct collection"/>
    {% endif %}
    </form>
    </fieldset>
    <!-- end collection correction -->
{% endif %}
{% if correction %} 
<a href="/phyloexplorer/download_correction"> Download corrections </a>
<br />
<br />
{% endif %}
{% if nb_taxa %}
<!-- stat1 -->
    <fieldset>
        <legend> Tree size distribution </legend>
        {{ tree_size_distributions|safe }}
    </fieldset>
<!-- end stat1 -->

<!-- stat2 -->
    <fieldset>
        <legend> Taxon frequency distribution </legend>
        <tt>
        {{taxon_frequency_distribution|safe}}
        </tt>
    </fieldset><br />
<!-- end stat2 -->
<fieldset><legend> {{taxo_engine|upper}} taxonomy excerpt from user taxa </legend>
<div id="tab_reference_tree">
    <ul>
        <li><a href="#arborescence"><span>Browsable arborescence</span></a></li>
        <li><a href="#image_tree"><span>Image</span></a></li>
        <li><a href="#source"><span>Newick format</span></a></li>
    </ul>
    <div id="arborescence">
	{% if display_reference %}
            <div id="reference_tree"> </div> <div id="progressbar_reference"></div> 
        {% else %}
		<a class="phylowidget" target="_blank" 
		href="http://www.phylowidget.org/full/index.html?tree='{{ reference_tree }}'"
		title="Display the ncbi tree as graph"> Click to visualize this tree using
		PhyloWidget </a> <img src="http://www.phylowidget.org/img/pw.ico" />
                <br />The browsable arborescence is too large to be dispayed in HTML (&gt; {{MAX_TAXA_DISPLAY_REFERENCE}}) .
        {% endif %}
    </div>
    <div id="image_tree">
        <div id="image_load">
            {% if nb_scientifics %}
            <a class="phylowidget" target="_blank" href="http://www.phylowidget.org/full/index.html?tree='{{ reference_tree }}'"
            title="Display the ncbi tree as graph"> Click to visualize this tree using PhyloWidget </a> <img src="/site_media/logo_pw.ico" />
            <br />
            {% endif %}
            <img class="img"></img></div>
        <span id="fetching_msg"><font color="red">Fetching image...</font></span>
    </div>
    <div id="source">
        <textarea cols=100 rows=30>{{reference_tree}}</textarea>
    </div>
</div>
</fieldset>
</div>
            <a href="#top" class="posPage">&nbsp;Go to the top of the page&nbsp;</a>
    <!-- div preview -->
    <div id="preview"></div>
{% endif %}
<div id="images"></div>
<!-- CONTENT : END -->
{% endif %}
{% endblock %}
